package com.wms.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wms.entity.Authority;
import com.wms.service.AuthorityService;
import com.wms.service.ResourceAuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/admin/authority")
@RestController("adminAuthorityController")
public class AuthorityController extends BaseController {

    @Autowired
    private AuthorityService authorityService;

    @Autowired
    private ResourceAuthorityService resourceAuthorityService;


    @RequestMapping("/list")
    public Object list(Page<Authority> page) {
        QueryWrapper<Authority> wrapper = new QueryWrapper<>();
        wrapper.ge("id", 0);
        IPage<Authority> iPage = authorityService.page(page, wrapper);
        return success(iPage);
    }

    @RequestMapping("/findAuthorityIdByResourceId")
    public Object findAuthorityIdByResourceId(Long resourceId) {
        if (resourceId == null){
            return error("参数不完整");
        }
        List<Long> authorityIds = resourceAuthorityService.findAuthorityIdByResourceId(resourceId);
        return success(authorityIds);
    }

    @RequestMapping("/save")
    public Object save(Authority authority) {
        if (StringUtils.isEmpty(authority.getName()) && StringUtils.isEmpty(authority.getUrl())){
            return error("参数不完整");
        }

        authorityService.save(authority);
        return success();
    }

    @RequestMapping("/detail")
    public Object detail(Long id) {
        if (id == null){
            return error("参数不完整");
        }

        Authority authority = authorityService.getById(id);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", authority.getId());
        map.put("url", authority.getUrl());
        map.put("name", authority.getName());
        map.put("createDate", authority.getCreateDate());
        return success(map);
    }

    @RequestMapping("/update")
    public Object update(Authority authority) {
        if (authority.getId() == null) return error("参数不完整");

        authorityService.updateById(authority);
        return success();
    }

    @RequestMapping("/delete")
    public Object delete(Long id){
        if (id == null) return error("参数不完整");
        authorityService.deleteById(id);
        return success();
    }

    @RequestMapping("/deleteBatch")
    public Object deleteBatch(@RequestParam(value="ids[]") List<Long> ids){
        if (ids == null || ids.size() == 0){
            return error("参数不完整");
        }

        authorityService.deleteByIds(ids);
        return success();
    }


}
